package com.ldc.wandroid.ui.fragments;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;

import com.ldc.common.mvvm.BaseMvvmFragment;
import com.ldc.common.utils.SPUtils;
import com.ldc.common.utils.StringUtils;
import com.ldc.common.utils.ToastUtils;
import com.ldc.common.utils.ViewModelUtils;
import com.ldc.common.utils.ViewUtils;
import com.ldc.wandroid.R;
import com.ldc.common.config.ArgParams;
import com.ldc.wandroid.contracts.RegisterContract;
import com.ldc.common.mvp.BaseFragment;
import com.ldc.wandroid.databinding.FragmentRegisterBinding;
import com.ldc.common.model.BaseModel;
import com.ldc.common.model.RegisterInfoModel;
import com.ldc.wandroid.ui.activitys.MainActivity;
import com.ldc.wandroid.viewmodel.RegisterViewModel;


public class RegisterFragment extends BaseMvvmFragment<FragmentRegisterBinding, RegisterViewModel> {

    private String userName;
    private String passWord;

    @Override
    public void onDestroy() {
        super.onDestroy();
        viewModel.getLoginInfo().removeObserver(this::registerData);
    }

    @Override
    protected FragmentRegisterBinding initViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return FragmentRegisterBinding.inflate(inflater, container, false);
    }

    @Override
    protected void main(Bundle savedInstanceState) {
        viewModel.getLoginInfo().observeForever(this::registerData);
        initTopBar();
        viewBinding.btnRegister.setOnClickListener(view -> {
            register();
        });
    }

    private void initTopBar() {
        viewBinding.topBar.lineMore.setVisibility(View.GONE);
        viewBinding.topBar.tvTitle.setText(R.string.str_register);
        viewBinding.topBar.lineBack.setOnClickListener(view -> {
            Navigation.findNavController(view)
                    .navigateUp();
        });
    }

    private void registerData(RegisterInfoModel registerInfoModel) {
        viewBinding.loading.viewPd.setVisibility(View.GONE);
        if (null == registerInfoModel) {
            return;
        }
        SPUtils.getInstance().putString(ArgParams.USER_NAME, userName);
        SPUtils.getInstance().putString(ArgParams.USER_PASSWORD, passWord);
        SPUtils.getInstance().putString(ArgParams.USER_INFO_ID, registerInfoModel.getId() + "");
        // 登录成功跳转主页
        MainActivity.start(requireContext());


    }

    @Override
    protected RegisterViewModel initViewModel() {
        return ViewModelUtils.getViewModel(this, RegisterViewModel.class);
    }

    private void register() {
        userName = viewBinding.etUserName.getText().toString();
        passWord = viewBinding.etUserPassword.getText().toString();
        String passWord2 = viewBinding.etUserPasswordAgain.getText().toString();
        if (StringUtils.isEmpty(userName)) {
            ToastUtils.showShort(getString(R.string.str_the_account_is_illegal));
            return;
        }
        if (StringUtils.isEmpty(passWord)) {
            ToastUtils.showShort(getString(R.string.str_the_password_is_illegal));
            return;
        }
        if (StringUtils.isEmpty(passWord2)) {
            ToastUtils.showShort(getString(R.string.str_the_password_is_illegal));
            return;
        }
        viewBinding.loading.viewPd.setVisibility(View.VISIBLE);
        viewModel.register(userName, passWord, passWord2);
    }
}
